#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 5;

int n, m;
int head[N], a[N], b[N];
int ver[N << 1], Next[N << 1], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}
int f[N], sz[N];
void dfs(int u, int fu) {
  f[u] = fu;
  sz[u] = 0;
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    if (v == fu) continue;
    sz[u]++;
    dfs(v, u);
  }
}
int cnt[N], now;
void add(int x) {
  if (cnt[x] == 0) now++;
  cnt[x]++;
  if (cnt[x] == 0) now--;
}
void del(int x) {
  if (cnt[x] == 0) now++;
  cnt[x]--;
  if (cnt[x] == 0) now--;
}
bool vis[N];
bool check() {
  now = 0;
  rep(i, 1, n) vis[i] = 0;
  queue<int> q;
  rep(i, 1, n) if (!sz[i]) q.push(i);
  while (q.size()) {
    int u = q.front();
    q.pop();
    do {
      vis[u] = 1;
      add(a[u]), del(b[u]);
      if (now == 0) {
        if (f[u] && !--sz[f[u]]) q.push(f[u]);
        break;
      }
    } while ((u = f[u]));
    if (now) return false;
  }
  return true;
}
void solve() {
  cin >> n;
  rep(i, 1, n) head[i] = 0;
  tot = 0;
  rep(i, 2, n) {
    int u, v;
    cin >> u >> v;
    add(u, v), add(v, u);
  }
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) cin >> b[i];
  rep(i, 1, n) cnt[a[i]] = cnt[b[i]] = 0;
  dfs(1, 0);
  if (check())
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}